package com.tbit.uqbike.webmanager.controller;

import cn.hutool.core.util.StrUtil;
import com.tbit.uqbike.annos.OperateLog;
import com.tbit.uqbike.constant.UserConstant;
import com.tbit.common.constant.MethodEnum;
import com.tbit.common.constant.ModuleEnum;
import com.tbit.uqbike.manager.exception.BaseException;
import com.tbit.uqbike.object.pojo.LessIntelligentSite;
import com.tbit.uqbike.object.pojo.ParkPoint;
import com.tbit.uqbike.object.value.LoginInfo;
import com.tbit.uqbike.object.view.JsonResult;
import com.tbit.uqbike.service.base.ParkPointService;
import com.tbit.uqbike.service.business.LessIntelligentSiteService;
import com.tbit.uqbike.webmanager.util.Assert;
import com.tbit.uqbike.webmanager.util.StringUtil;
import com.tbit.utils.DateTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotNull;
import java.util.List;


/**
 * @author ：hzw
 * @date ：Created in 2021/8/30 16:44
 * @description：冷门站点推荐控制层
 * @modified By：
 * @version: $
 */
@RestController
@RequestMapping(value = "/lessIntelligentSite")
public class LessIntelligentSiteController extends BaseController {
    @Autowired
    private LessIntelligentSiteService lessIntelligentSiteService;
    @Autowired
    private ParkPointService parkPointService;

    @RequestMapping(value = "/queryPage")
    public JsonResult queryPage(@RequestParam(defaultValue = "10") Integer rowCount,
                                @RequestParam(defaultValue = "1") Integer pageNO,
                                String regionIds, Integer state, Long startTime, Long endTime, Integer accountId) {
        List<Integer> regionIdList = StringUtil.getIntegerList(regionIds);

        List<LessIntelligentSite> list = lessIntelligentSiteService.queryPage(startTime, endTime, state, regionIdList, pageNO, rowCount, accountId);
        for (LessIntelligentSite lessIntelligentSite : list) {
            /**根据id获取站点*/
            ParkPoint parkPoint = parkPointService.getByParkPointId(lessIntelligentSite.getSiteId());
            if (parkPoint != null) {
                /**拼接数据*/
                lessIntelligentSite.setSiteName(parkPoint.getName());
                /**如果校准后的经纬度为空，则拿未校准的经纬度*/
                if (null == parkPoint.getLaC() || null == parkPoint.getLoC()) {
                    lessIntelligentSite.setLaC(parkPoint.getLa());
                    lessIntelligentSite.setLoC(parkPoint.getLo());
                } else {
                    lessIntelligentSite.setLaC(parkPoint.getLaC());
                    lessIntelligentSite.setLoC(parkPoint.getLoC());
                }

                if (null != parkPoint.getPointsC()) {
                    lessIntelligentSite.setPoints(parkPoint.getPointsC());
                }
            }
        }

        Integer count = lessIntelligentSiteService.queryCount(startTime, endTime, state, regionIdList, accountId);

        return JsonResult.ok(list, count);
    }

    /* 处理 */
    @RequestMapping("/handle")
    @OperateLog(module = ModuleEnum.LESSINTELLIGENTSITE, methods = MethodEnum.LESSINTELLIGENTSITE_HANDLE)
    public JsonResult handle(@NotNull(message = "ver.siteId.notNull") Integer siteId,
                             String auditReason, Integer agree, Integer accountId, String remark,
                             HttpServletRequest request) {

        LoginInfo loginInfo = getLogin(request);
        if (loginInfo.type.equals(UserConstant.USER_AGENT)) {
            accountId = loginInfo.id;
        } else {
            Assert.notNull(accountId, "ver.accountId .notNull");
        }
        if (StrUtil.isNotEmpty(auditReason) && auditReason.length() > 50){
            throw new BaseException("审核理由过长");
        }else if (StrUtil.isNotEmpty(remark) && remark.length() > 50){
            throw new BaseException("审核备注过长");
        }
        LessIntelligentSite lessIntelligentSite = new LessIntelligentSite();
        lessIntelligentSite.setSiteId(siteId);
        lessIntelligentSite.setAuditReason(auditReason);
        lessIntelligentSite.setAuditTime(DateTimeUtil.getNowTime());
        lessIntelligentSite.setState(1);
        lessIntelligentSiteService.handle(lessIntelligentSite);
        /**同意后根据推荐站点去删除指定站点*/
        if (accountId != null && agree == 1) {
            lessIntelligentSite = lessIntelligentSiteService.getLotIntelligentSite(siteId);
            parkPointService.del(lessIntelligentSite.getSiteId(), loginInfo);
        }
        return JsonResult.ok();
    }

}
